function redirectToPage (redirectUrl) {
	return {
		type: 'APP_REDIRECT_USER',
		payload: redirectUrl
	}
}

function loginWithFacebook (facebookId, facebookToken) {
	return createAsyncAction('APP_LOGIN_WITH_FACEBOOK', () => {
		api.post('/auth/facebook', {
			facebook_id: facebookId,
			facebook_token: facebookToken
		})
	})
}

function signupWithFacebook (facebookId, facebookToken, facebookEmail) {
	if (!facebookEmail) {
		redirectToPage('/fill-in-details')
	}

	return createAsyncAction('APP_SIGNUP_WITH_FACEBOOK', () => {
		api.post('/accounts', {
			authentication_type: 'facebook',
			facebook_id: facebookId,
			facebook_token: facebookToken,
			email: facebookEmail
		})
	})
}

function connectWithFacebook (facebookId, facebookToken, facebookEmail) {
	const firstAction = loginWithFacebook(facebookId, facebookToken)
	const callbackAction = signupWithFacebook(facebookId, facebookToken, facebookEmail)

	const handlers = [{
		status: 'success',
		callback: () => (() => {}) // 用户登陆成功
	}, {
		status: 'error',
		callback: callbackAction, // // 使用 facebook 账户登陆失败，尝试帮用户注册新账户
	}]

	return createChainedAsyncAction(firstAction, handlers)
}


// 直接登录成功
const APP_LOGIN_WITH_FACEBOOK_REQUEST = 'APP_LOGIN_WITH_FACEBOOK_REQUEST'
const APP_LOGIN_WITH_FACEBOOK_SUCCESS = 'APP_LOGIN_WITH_FACEBOOK_SUCCESS'

// 直接登录失败，注册信息充足
const APP_LOGIN_WITH_FACEBOOK_REQUEST = 'APP_LOGIN_WITH_FACEBOOK_REQUEST'
const APP_LOGIN_WITH_FACEBOOK_ERROR = 'APP_LOGIN_WITH_FACEBOOK_ERROR'
const APP_SIGNUP_WITH_FACEBOOK_REQUEST = 'APP_SIGNUP_WITH_FACEBOOK_REQUEST'
const APP_LOGIN_WITH_FACEBOOK_SUCCESS = 'APP_LOGIN_WITH_FACEBOOK_SUCCESS'

// 直接登录失败，注册信息不足
const APP_LOGIN_WITH_FACEBOOK_REQUEST = 'APP_LOGIN_WITH_FACEBOOK_REQUEST'
const APP_LOGIN_WITH_FACEBOOK_ERROR = 'APP_LOGIN_WITH_FACEBOOK_ERROR'
const APP_REDIRECT_USER = 'APP_REDIRECT_USER'